package com.lzj.demo.base;

import java.util.*;


enum Color {

    SPADE,      // ♠

    REAHEART,   // ♥

    BLOCK,      // ♦

    CLUB,       // ♣

}

enum Number {
    THREE,
    FOUR,
    FIVE,
    SIX,
    SEVEN,
    EIGHT,
    NINE,
    TEN,
    J,
    Q,
    K,
    A,
    TWO,
    BIGKING,
    SMALLKING
}

/*
 * 一张牌
 * */
class Card implements Comparable<Card> {
    private Color color;
    private Number number;

    public Card(Color color, Number number) {
        this.color = color;
        this.number = number;
    }

    public char getColor() {
        switch (this.color) {
            case SPADE:
                return '♠';
            case REAHEART:
                return '♥';
            case CLUB:
                return '♦';
            case BLOCK:
                return '♣';
        }
        return ' ';
    }

    public void setColor(Color color) {
        this.color = color;
    }

    public String getNumber() {
        switch (this.number) {
            case TWO:
                return "2";
            case THREE:
                return "3";
            case FOUR:
                return "4";
            case FIVE:
                return "5";
            case SIX:
                return "6";
            case SEVEN:
                return "7";
            case EIGHT:
                return "8";
            case NINE:
                return "9";
            case TEN:
                return "10";
            case J:
                return "J";
            case Q:
                return "Q";
            case K:
                return "K";
            case A:
                return "A";
            case SMALLKING:
                return "🐖";
            case BIGKING:
                return "🐂";
        }
        return "";
    }

    public void setNumber(Number number) {
        this.number = number;
    }

    public String read() {
        return this.getColor() + ": " + this.getNumber();
    }

    @Override
    public int compareTo(Card o) {
        return this.read().compareTo(o.read());
    }

    @Override
    public String toString() {
        return this.read();
    }
}

/*
 * 玩家
 * */
class Player {
    private Set<Card> cards;
    private String name;

    public Player() {
        this.cards = new TreeSet<>();
    }

    public Player(String name) {
        this.name = name;
        this.cards = new TreeSet<>();
    }

    public void clear() {
        this.cards.clear();
    }

    public void play(Card card) {
        this.cards.remove(card);
    }

    public boolean touchCard(Card card) {
        System.out.println(this.name + "抓了一张牌");
        return this.cards.add(card);
    }

    public String readCard() {
        StringBuilder res = new StringBuilder("[ ");
        for (Card card : this.cards) {
            res.append(card)
               .append(", ");
        }
        return res.substring(0, res.length() - 2) + "]";
    }
}


public class PoKerDemo {
    private List<Card> allCards;
    private int index;

    public PoKerDemo() {
        this.allCards = new ArrayList<>();
        // 构造一副牌
        for (Color color : Color.values()) {
            for (Number number : Number.values()) {
                this.allCards.add(new Card(color, number));
            }
        }

        this.shuffle();
    }


    public void shuffle() {
        System.out.println("美女健哥在线洗牌");
        this.index = this.allCards.size() - 1;
        Collections.shuffle(this.allCards);
    }

    public boolean canTouch() {
        return this.index >= 3;
    }

    public Card touch() {
        // 如果可以摸牌
        if (canTouch()) {
            return this.allCards.get(this.index --);
        } else {
            return null;
        }
    }

    public List<Card> getBottom() {
        // TODO: 判断现在能不能看底牌
        List<Card> bottomCard = new ArrayList<>();
        bottomCard.add(this.allCards.get(0));
        bottomCard.add(this.allCards.get(1));
        bottomCard.add(this.allCards.get(2));
        return bottomCard;
    }

    /*
     * 把一副牌发给三个人
     * */
    public static void battleLandMaster(Player p1, Player p2, Player p3, PoKerDemo poKer) {
        while (poKer.canTouch()) {
            // 从牌堆抓一张
            Card card = poKer.touch();
            // 放入到手上的牌中
            p1.touchCard(card);

            // 从牌堆抓一张
            card = poKer.touch();
            // 放入到手上的牌中
            p2.touchCard(card);

            // 从牌堆抓一张
            card = poKer.touch();
            // 放入到手上的牌中
            p3.touchCard(card);
        }
        // TODO: 选一个地主
        Random random = new Random();
        switch (random.nextInt() % 3) {
            case 0: {
                // TODO:
                break;
            }
            case 1: {
                // TODO:
                break;
            }
            case 2: {
                // TODO:
                break;
            }

        }

    }

    public static void main(String[] args) {
        // 三个玩家
        Player p1 = new Player("张三");
        Player p2 = new Player("王八");
        Player p3 = new Player("李六");
        // 一副牌
        PoKerDemo poKer = new PoKerDemo();
        PoKerDemo.battleLandMaster(p1, p2, p3, poKer);
        System.out.println(p1.readCard());
        System.out.println(p3.readCard());
        System.out.println(p2.readCard());
//
//        Collections.shuffle(array);
//        TreeSet<Integer> lzPoKe = new TreeSet<>();
//        TreeSet<Integer> zzPoKe = new TreeSet<>();
//        TreeSet<Integer> jjPoKe = new TreeSet<>();
//        TreeSet<Integer> dpPoKe = new TreeSet<>();
//        for (int i = 0; i < array.size(); i++) {
//            int x = array.get(i);
//            if (i >= array.size() - 3) {
//                dpPoKe.add(x);
//            } else if (i % 3 == 0) {
//                lzPoKe.add(x);
//            } else if (i % 3 == 1) {
//                zzPoKe.add(x);
//            } else if (i % 3 == 2) {
//                jjPoKe.add(x);
//            }
//
//        }
//        LookPoKer("ll", lzPoKe, hm);
//        LookPoKer("zz", zzPoKe, hm);
//        LookPoKer("jj", jjPoKe, hm);
//        LookPoKer("底牌", dpPoKe, hm);

    }

    public static void LookPoKer(String name, TreeSet<Integer> ts, HashMap<Integer, String> hm) {
        System.out.print(name + " 的牌是：");

        for (Integer key : ts) {
            String poker = hm.get(key);
            System.out.print(poker);
        }
        System.out.println();
    }
}
